<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/alerter/modules/base_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/alerter/modules/watched.php");

	class WatchedMod extends AlertBaseMod {
	
		public static function getWatchedPilot($name) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from watched_pilots wp where wp.name = '%s'",
					mysql_real_escape_string($name)
				);
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new Watched(
					$row["id"],
					$row["pid"],
					$row["name"],
					$row["faction"],
					$row["alliance"],
					$row["alerter"]
				);
		}
		
		public static function getWatchedPilotById($id) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from watched_pilots where id = %d",
					$id
				);
			//echo $sql;
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new Watched(
					$row["id"],
					$row["pid"],
					$row["name"],
					$row["faction"],
					$row["alliance"],
					$row["alerter"]
				);
		}
		
		public static function getWatchedPilotByPid($pid) {
			$conn = self::getConnection();
			$sql =
				sprintf(
					"select * from watched_pilots where pid = %d",
					$pid
				);
			//echo $sql;
			$row = mysql_fetch_assoc(mysql_query($sql, $conn)); 
			if (!$row)
				return null;
			return
				new Watched(
					$row["id"],
					$row["pid"],
					$row["name"],
					$row["faction"],
					$row["alliance"],
					$row["alerter"]
				);
		}

		public static function getWatchedPilots(&$pageNumber, &$pageCount) {
			$conn = self::getConnection();

			$sql = "select count(*) as cnt from watched_pilots";
			$result = mysql_query($sql, $conn);
			$row = mysql_fetch_assoc($result);
			$recordCount = $row["cnt"];
			$pageCount = ceil($recordCount / SettingsMod::PAGE_RECORDS_PER_PAGE);
			if ($pageNumber > $pageCount)
				$pageNumber = $pageCount;


			if ($pageNumber < $pageCount)
				$recordsPerPage = SettingsMod::PAGE_RECORDS_PER_PAGE;
			else {
				$recordsPerPage = $recordCount % SettingsMod::PAGE_RECORDS_PER_PAGE;
				if ($recordsPerPage == 0 && $recordCount > 0)
					$recordsPerPage = SettingsMod::PAGE_RECORDS_PER_PAGE; 
			}
			$sql =
				sprintf(
					"select * from ( " .
						"select * from (" .
							"select * from watched_pilots " .
							"order by `name` asc " .
							"limit 0, %d" .
						") as tmp1 " .
						"order by `name` asc " .
						"limit 0, %d " .
					") as tmp2 " .
					"order by `name` asc",
					SettingsMod::PAGE_RECORDS_PER_PAGE * $pageNumber,
					$recordsPerPage
				);
			$watched = array();
			$result = mysql_query($sql, $conn);
			while ($row = mysql_fetch_assoc($result)) {
				$wtc =
					new Watched(
						$row["id"],
						$row["pid"],
						$row["name"],
						$row["faction"],
						$row["alliance"],
						$row["alerter"]
					);
				$watched [] = $wtc;
			}
			return $watched ;
		}

		public static function addWatched($wtc) {
			// ONLY add unique pilot ids
			if (!self::getWatchedPilotByPid($wtc->getPid())){
				$conn = self::getConnection();
				$sql = sprintf(
					"insert into watched_pilots (pid, name, faction, alliance, alerter) values (%d, '%s', '%s', '%s', '%s')",
					$wtc->getPid(),
					mysql_real_escape_string($wtc->getName()),
					mysql_real_escape_string($wtc->getFaction()),
					mysql_real_escape_string($wtc->getAlliance()),
					mysql_real_escape_string($wtc->getAlerter())
				);
				//echo $sql;
				mysql_query($sql, $conn);
			}
		}

		public static function deleteWatchedById($id) {
			$conn = self::getConnection();
			$sql = sprintf(
				"delete from watched_pilots where id= %d",
				$id
				);
			//echo "<script>alert('".$sql."')</script>";
			mysql_query($sql, $conn);
		}
		
		public static function updateWatched($wtc) {
			$conn = self::getConnection();
			$sql = sprintf(
				"update watched_pilots 
				set pid = %d, name = '%s', faction = '%s', alliance = '%s', alerter = '%s' 
				where id = %d",
				$wtc->getPid(),
				mysql_real_escape_string($wtc->getName()),
				mysql_real_escape_string($wtc->getFaction()),
				mysql_real_escape_string($wtc->getAlliance()),
				mysql_real_escape_string($wtc->getAlerter()),
				$wtc->getId()
			);
			//echo $sql;
			mysql_query($sql, $conn);
		}

	}
?>